package com.controller;

import com.entity.User;
import com.sun.net.httpserver.HttpExchange;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException
    {
        req.getRequestDispatcher("/WEB-INF/jsp/login.jsp")
                .forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
    {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String url;
        if(username.equals("admin")&&password.equals("admin")) {
            User user = new User("admin");
            req.getSession().setAttribute("user",user);
            url ="/filter/houtai";
        }
        else {
            url = "/login";
        }
        resp.sendRedirect(resp.encodeRedirectURL(req.getContextPath() + url));
    }

}
